<template>
	<Layout class="p-5 bg-white">
		<Form :label-width="100">
			<Row class="p-1">
				<Col span="24">
					<FormItem class="w-50" label="分类名称">
						<Input v-model="category.name" placeholder="请输入分类名称"></Input>
						
					</FormItem>
					
					<FormItem class="w-50" label="分类状态">
						<Select v-model="category.statu" clearable >
							<Option value="1">激活</Option>
							<Option value="0">禁用</Option>
						</Select>
					</FormItem>
					
					<FormItem class="w-50" label="当前分类等级:" clearable >
						<label>{{category.level}}</label>
						
					</FormItem>
					
					<FormItem v-if="category.config" class="w-50" label="父级分类">
						
						<Select  v-if="category.level > 1" @on-change="loadCategorys(item.categoryId, item.value)" v-for="(item, index) in this.categorysArrays" :key="index" v-model="item.value">
							<Option v-for="(category, jndex) in item.source" :key="jndex" :value="category.categoryId">{{category.name}}</Option>
						</Select>
						<Select v-if="category.level < 1" clearable >
							<Option value="null">无匹配数据</Option>
						</Select>
						
					</FormItem>
					<FormItem class="w-50" label="">
						<Button  type="primary" style="float: right;" @click="removePs">重置分类</Button>
						<Button  type="error" style="float: right;" @click="changeLevel">创建首页一级分类</Button>
					</FormItem>
					
					
					<FormItem class="w-50" label="分类封面">
						<div class="d-flex flex-row">
							<label class="ivu-input">{{coverFile.name}}</label>
							<label style="line-height: 30px;" class="ivu-btn ivu-btn-default ml-1" for="cover">选择文件</label>
							<input @change="fileChange($event,coverFile)" id="cover" style="display: none;" type="file"></input>
						</div>
						<div class="d-flex flex-row mt-2">
							<img v-if="coverFile.url != ''" class="w-50" :src="coverFile.url" />
						</div>
					</FormItem>
					
				</Col>
				
			</Row>
			<Row class="p-1">
				<Col span="24">
					<FormItem class="" label="图文内容">
						<div style="display: block; position: relative; z-index: 0;" id="editor">
							
						</div>
					</FormItem>
				</Col>
			</Row>
			<Row class="p-1">
				<Col span="24">
					<FormItem class="text-right">
						<Button @click="submit" type="primary">提交</Button>
						<Button @click="cancel" class="ml-2">取消</Button>
					</FormItem>
				</Col>
			</Row>
	
		</Form>
	</Layout>
</template>

<script>
	import E from "wangeditor";
	
	export default {
		data() {
			return {
				category: {
					categoryId: "",
					name: "",
					level: "2",
					pId: "",
					statu: "1",
					imgId: "",
					config:true
				},
				image: {
					imgId: "",
					url: "",
					statu: "",
					dFlg: ""
				},
				coverFile: {
					name: "",
					file: undefined,
					url:""
				},
				editor: undefined,
				contant: "",
				categorysArrays: [
					
				]
				
			}
		},
		methods: {
			changeLevel: function(){
				this.category.level = 1;
				this.category.config=false;
			},
			removePs: function(){
				this.category.config=true;
				this.psName = "";
				this.categorysArrays=[];
				this.loadCategorys(null, 0);
				this.category.level = 2;
			},
			pLevel: function(level){
				if(level == 1){
					this.category.level = level;
				}
			},
			loadCategorys: function(event, categoryId) {
				let targetIndex = undefined;
				let that = this;
				// console.log('++++++++++++++++')
				// console.log(this.categorysArrays)
				// console.log(event)
				// console.log(categoryId)
				for (let index = 0; index < this.categorysArrays.length; index++) {
					let targetCategorys = this.categorysArrays[index];
					if (event === targetCategorys.categoryId) {
						targetIndex = index;
						break;
					}
				}
				
				targetIndex = targetIndex + 1;
				
				if (targetIndex !== undefined) {
					this.categorysArrays.splice(targetIndex, this.categorysArrays.length - targetIndex);
					
				}
				
				// if (categoryId !== null && categoryId !== undefined && categoryId !== "" && categoryId !== 0) {
					// console.log('/////')
					this.http.get({
						url: "/product/category/info",
						param: {
							categoryId
						}
					}).then(res => {
						
						let categorysSource = {
							categoryId,
							value: "",
							source: res.category.subCategory
						};
						this.categorysArrays.push(categorysSource);
					});
				// }
				this.category.pId = categoryId;
				if (categoryId !=0){
					let self = this;
					//查询
					this.http.get({
						url: "/product/category/info",
						param: {
							categoryId: categoryId
						}
					}).then(data => {
						self.category.level = data.category.level + 1;
					});
					
				}
								
			},
			fileChange: function(e,imgItem) {
				
				
				imgItem.file = e.target.files[0];
				imgItem.name = imgItem.file.name;
				
				let reader = new FileReader();
				reader.onloadend = function() {
					imgItem.url = this.result;
					this.url = this.result;
				}
				reader.readAsDataURL(imgItem.file);
				imgItem.onProgress = this.onProgress;
			},
			onProgress: function(pData) {
				console.log(pData);
			},
			submit: function() {
				let self = this;
				if (this.category.name.length === 0) {
					this.message.error("请输入分类名称");
					return;
				}
				if (this.category.statu.length === 0) {
					this.message.error("请选择分类状态");
					return;
				}
				if (this.category.level < 1) {
					this.message.error("分类等级默认大于0");
					return;
				}
				if (this.category.level.length === 0) {
					this.message.error("请选择分类等级");
					return;
				}
				if (this.category.pId.length === 0) {
					this.message.error("请输入父级分类");
					return;
				}
				if (this.common.isEmptyString(this.coverFile.name)) {
					this.message.warning("请上传分类封面图片");
					return;
				}
				if (this.category.pId === 0 && this.category.level!=1) {
					this.message.error("请选择父级");
					return;
				}
				
				this.common.submitFile(this.coverFile).then(function(res) {
					self.coverFile.url = res.Location;
					// console.log(self.coverFile.url);
					self.http.post({
						url: "/product/category/new",
						param: {
							name: self.category.name,
							statu: self.category.statu,
							level: self.category.level,
							pId: self.category.pId,
							url:self.coverFile.url,
							contant: self.editor.txt.html(),
						}
					}).then(data => {
						self.message.info("添加成功");
						self.$tabs.close();
					});
				}).catch(function(err) {
					self.message.error("封面文件上传失败，取消新增产品。");
				});	
			},
			// 失去焦点
			onEditorBlur(editor) {},
			// 获得焦点
			onEditorFocus(editor) {},
			// 开始
			onEditorReady(editor) {},
			// 值发生变化
			onEditorChange(editor) {
				this.contant = editor.html;
			},
			cancel: function() {
				this.$tabs.close();
			},
			init: function() {
				this.loadCategorys(null, 0);
			}
		},
		
		mounted() {
			this.editor = new E("#editor");
			
			let self = this;
			this.editor.config.customUploadImg = this.common.editorUploadFile;
			this.editor.create();
		},
		created: function(){
			this.init();
		}
	}
</script>

<style>
</style>
